package com.bjtu.literatureclient.controller.category;

import com.bjtu.literatureclient.domain.category.SubsetIndexNo;
import com.bjtu.literatureclient.domain.user.User;
import com.bjtu.literatureclient.domain.user.UserIndexNoTable;
import com.bjtu.literatureclient.service.category.ICategorySrevice;
import com.google.gson.Gson;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
*@author LMR
*@data 2018年1月30日下午8:46:16
*/
@RestController
@RequestMapping("/category")
public class CategoryController {

	 @Autowired
	    private ICategorySrevice categoryService;
	 /*
	 	查询所有分类
	  */
	 @RequestMapping(value = "/query", method = RequestMethod.POST)
	 public String queryCategory(){
		 Subject currentUser = SecurityUtils.getSubject();
		 Session session = currentUser.getSession();
		 User user=(User) session.getAttribute("userInfo");
		 List<SubsetIndexNo> categoryList = categoryService.queryCategory(user.getUserNo());
		 if(categoryList.size()==0){
			 SubsetIndexNo subset =new SubsetIndexNo(user.getUserNo(),null,null,null,null);
		 	categoryList.add(subset);
		 }
		 Gson gson = new Gson();
	     return gson.toJson(categoryList);
	 }
	 
	 @RequestMapping(value = "/query/{param}", method = RequestMethod.GET)
	 public String queryMajor(@PathVariable("param") String param){
		 System.out.println(param);
		 SubsetIndexNo subset = new SubsetIndexNo();
		 subset.setSubsetTitle(param);
		 List<SubsetIndexNo> categoryList = categoryService.queryMajor(subset);
		 Gson gson = new Gson();
	     return gson.toJson(categoryList);
		 
	 }
	@RequestMapping(value = "/getSubsetNoNew", method = RequestMethod.POST)
	public String getSubsetNoNew(){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user=(User) session.getAttribute("userInfo");
		String subsetNoNew = categoryService.queryNewSubsetNo(user.getUserNo());
		Gson gson = new Gson();
		return gson.toJson(subsetNoNew);

	}
	@RequestMapping(value = "/addNewCategory", method = RequestMethod.POST)
	public String addNewCategory(@RequestBody SubsetIndexNo subset){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user=(User) session.getAttribute("userInfo");
		String subsetNo = subset.getSubsetNo();
		String subsetTitle = subset.getSubsetTitle();
		int flag = categoryService.addNewCategory(user.getUserNo(),subsetNo,subsetTitle);
		Map<String, String> resultMap = new HashMap();
		if (flag == 1) {
			resultMap.put("data", "success");
		} else if(flag ==999){
			resultMap.put("data", "该名字已存在");
		} else{
			resultMap.put("data", "error");
		}
		Gson gson = new Gson();
		return gson.toJson(resultMap);

	}

	/*
		根据自己查询文献
	 */
	@RequestMapping(value = "/queryBySet", method = RequestMethod.POST)
	public String queryBySet(@RequestParam String userNo, @RequestParam String subsetNo){
//		String userNo = request.getParameter("userNo");
//		String subsetNo = request.getParameter("subsetNo");
		List<UserIndexNoTable> subsets = categoryService.queryBySet(userNo,subsetNo);
		Gson gson = new Gson();
		return gson.toJson(subsets);
	}

	/*
		删除分类类别
	 */
	@RequestMapping(value = "/deleteCategory", method = RequestMethod.POST)
	public String deleteCategory( @RequestBody Map<String, Object> params){
		String userNo = (String) params.get("userNo");
		String subsetNo = (String) params.get("subsetNo");
		boolean bo = categoryService.deleteCategory(userNo,subsetNo);
		Gson gson = new Gson();
		return gson.toJson(bo);
	}
	/*
		查询该用户的所有参考文献
	 */
	@RequestMapping(value = "/queryAllreference", method = RequestMethod.POST)
	public String queryAllreference(@RequestParam String userNo, @RequestParam String subsetNo){
		List<UserIndexNoTable> allReference= categoryService.queryAllreference(userNo);
		List<UserIndexNoTable> alreadyReference = categoryService.queryBySet(userNo,subsetNo);
		allReference.removeAll(alreadyReference);
		Gson gson = new Gson();
		return gson.toJson(allReference);
	}
	@RequestMapping(value = "/addPapersToGroup", method = RequestMethod.POST)
	public String addPapersToGroup(@RequestBody SubsetIndexNo[] subsets) throws JSONException {
		System.out.println(subsets.length);
		for(int i=0;i<subsets.length;i++){
			Date d=new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//格式化为日期/时间字符串
			String stamp = sdf.format(d);
			subsets[i].setSubsetDate(stamp);
		}

		boolean bo= categoryService.addPapersToGroup(Arrays.asList(subsets));
		Gson gson = new Gson();
		return gson.toJson(null);
	}

	/*
    删除分类类别
 */
	@RequestMapping(value = "/deletePaperFromGroup", method = RequestMethod.POST)
	public String deletePaperFromGroup( @RequestBody Map<String, Object> params){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user=(User) session.getAttribute("userInfo");
		String indexNo = (String) params.get("indexNo");
		String subsetNo = (String) params.get("subsetNo");
		boolean bo = categoryService.deletePaperFromGroup(user.getUserNo(),indexNo,subsetNo);
		Gson gson = new Gson();
		return gson.toJson(bo);
	}
}
